import django

from app.FileTypeEnum import FileTypeEnum
from app.models import *
from app.statusenum import ProjectStatusEnum, GraphStatusEnum
from enum import unique, IntEnum


@unique
class CopyGraphStatus(IntEnum):
    FAIL = 1
    SUCCESS = 0


# 复制项目所有图
def copy_project_graphs(old_project_id, new_project_id):
    """
    :param old_project_id: 需要复制的图的所在项目id
    :param new_project_id: 复制到的项目id
    :return: 复制是否成功
    """
    # 获取该项目所有图的列表
    try:
        graph_list = Graphs.objects.filter(project_id=old_project_id)
    except Graphs.DoesNotExist:
        return CopyGraphStatus.FAIL.value
    try:
        old_project = Projects.objects.get(project_id=old_project_id)
    except Projects.DoesNotExist:
        return CopyGraphStatus.FAIL.value
    try:
        new_project = Projects.objects.get(project_id=new_project_id)
    except Projects.DoesNotExist:
        return CopyGraphStatus.FAIL.value
    # 更新临时项目
    new_project.update_time = django.utils.timezone.now()
    new_project.save()
    # 遍历复制图
    for graph in graph_list:
        new_graph = Graphs(graph_name=graph.graph_name, project_id=new_project.project_id,
                           graph_status=GraphStatusEnum.NORMAL.value,
                           create_time=django.utils.timezone.now(), update_time=django.utils.timezone.now(),
                           graph_data=graph.graph_data, graph_type=graph.graph_type, graph_info=graph.graph_info)
        new_graph.save()
    # 返回复制project的id
    return CopyGraphStatus.SUCCESS.value
